refactor: validateTokenAndCheckExpiration simplified and added tests #455
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



What is Changed / Added
AuthService.getAuthDetailsnow properly handles the case where the backend detects an old token version during the call torefreshUserToken. When this specific error occurs, the system automatically clears the stored user credentials and throws a dedicatedOldTokenDetectedError. This change aims to prevents users from getting stuck in a broken authentication state where their local token is no longer valid on the server.In the following trace you can see the error I am trying to fix with this pull request
{ "level": "error", "message": "[REPORTED_ERROR]: Old token version detected\nProperties => {\n \"command\": \"webdav\"\n}\nStack => Error: Old token version detected\n at new AppError (/usr/lib/node_modules/@internxt/cli/node_modules/@internxt/sdk/dist/shared/types/errors.js:21:28)\n at HttpClient.normalizeError (/usr/lib/node_modules/@internxt/cli/node_modules/@internxt/sdk/dist/shared/http/client.js:190:15)\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async Axios.request (/usr/lib/node_modules/@internxt/cli/node_modules/axios/dist/node/axios.cjs:4478:14)\n at async AuthService.refreshUserToken (/usr/lib/node_modules/@internxt/cli/dist/services/auth.service.js:79:26)\n at async AuthService.getAuthDetails (/usr/lib/node_modules/@internxt/cli/dist/services/auth.service.js:54:26)\n at async Webdav.run" }The method in charge of validating and checking the expiration of the token
validateTokenAndCheckExpirationwas refactored to separate concerns more clearly. This way it is easier to test and reason about token states throughout the authentication flow.Two new error detection utilities
isFileNotFoundErrorandisOldTokenErrorwere added to identify file system errors and old token errors more reliably.ConfigService.clearUserwas also updated to gracefully handle cases where the credentials file doesn't exist during cleanup operations.